//
//  AppDelegate.m
//  唧歪
//
//  Created by MWJ on 15/7/6.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "LeftViewController.h"
#import "Reachability.h"


WnsSDK *gWnsSDK = nil;
WnsSDKStatus gWnsSDKStatus = WnsSDKStatusDisconnected;

@interface AppDelegate (){
    UIScrollView *_sView;
    UIScrollView *sView;
}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    gWnsSDK = [[WnsSDK alloc] initWithAppID:202169
                              andAppVersion:@"1.0"
                              andAppChannel:@"rdm"];
    [gWnsSDK setStatusCallback:^(WnsSDKStatus status) {
//        NSLog(@"WnsSDKStatus:%ld", (long)status);
        gWnsSDKStatus = status;
        
    }];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];   //设置通用背景颜色
    [self.window makeKeyAndVisible];
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *mainVC = [sb instantiateViewControllerWithIdentifier:@"homeVC"];
    self.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
    LeftViewController *leftVC = [sb instantiateViewControllerWithIdentifier:@"leftVC"];
    self.LeftSlideVC = [[LeftSlideViewController alloc] initWithLeftView:leftVC andMainView:self.mainNavigationController];
    self.window.rootViewController = self.LeftSlideVC;
    
//    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:215.0/255.0 blue:0.0/255.0 alpha:1.0]];
    [self.mainNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.jpg"] forBarMetrics:UIBarMetricsDefault];
//    [[UINavigationBar appearance] setBarTintColor:[UIColor grayColor]];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self setupD];
    
    return YES;
}

-(void)setupD{
#pragma mark 【第一次启动调用引导页】
    /*判断第一次启动的时候是否添加*/
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults boolForKey:@"one"]) {
        NSLog(@"不是第一次启动");
    }else{
        [userDefaults setBool:YES forKey:@"one"];
        [userDefaults synchronize];
        
        sView = [[UIScrollView alloc]init];
        sView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        sView.contentSize =CGSizeMake(3*[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        _sView=sView;
        UIImageView *imageView1 = [[UIImageView alloc]init];
        imageView1.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        NSURL *url1= [NSURL URLWithString:@"http://192.168.1.103:8888/Images/STAR1.png"];
        imageView1.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url1]]];
        [sView addSubview:imageView1];
        
        UIImageView *imageView2 = [[UIImageView alloc]init];
        imageView2.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        NSURL *url2= [NSURL URLWithString:@"http://192.168.1.103:8888/Images/STAR2.jpg"];
        imageView2.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url2]]];

        [sView addSubview:imageView2];
        
        UIImageView *imageView3 = [[UIImageView alloc]init];
        imageView3.frame = CGRectMake(2*[UIScreen mainScreen].bounds.size.width,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        NSURL *url3= [NSURL URLWithString:@"http://192.168.1.103:8888/Images/STAR3.png"];
        imageView3.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url3]]];

        [sView addSubview:imageView3];
        
        UIButton *jinru = [[UIButton alloc]init];
        jinru.frame = CGRectMake(2.5*[UIScreen mainScreen].bounds.size.width-80, [UIScreen mainScreen].bounds.size.height-100, 160, 30);
        [jinru setTitle:@"开始体验" forState:UIControlStateNormal];
//        [jinru setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [jinru setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [jinru addTarget:self action:@selector(Clickon) forControlEvents:UIControlEventTouchUpInside];
        [sView addSubview:jinru];
        
        
        sView.pagingEnabled=YES;
        sView.showsHorizontalScrollIndicator=NO;
        sView.delegate =self;
        
        
        [self.window addSubview:sView];
        
        
        [sView scrollRectToVisible:CGRectMake(0, 0, sView.frame.size.width, sView.frame.size.height) animated:YES];
    }
    
}
-(void)Clickon{
    [sView removeFromSuperview];
    
}
-(BOOL) isConnectionAvailable{
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            //            NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //            NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //            NSLog(@"3G");
            break;
    }
    if (!isExistenceNetwork) {
        HUD = [[MBProgressHUD alloc] initWithView:self.window];
        [self.window addSubview:HUD];
        
        HUD.delegate = self;
        [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
        self.Existence = @"1";
    }
    
    return isExistenceNetwork;
}
- (void)myTask {
    sleep(5);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
